package com.mxfcode.back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mxfcode.back.dto.PermissionDTO;
import com.mxfcode.back.mapper.PermissionMapper;
import com.mxfcode.back.service.PermissionService;
import com.mxfcode.bbs_common.date.DateUtils;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.common.HostHolder;
import com.mxfcode.entity.TSPermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名 PermissionServiceImpl
 * @描述 权限业务
 * @作者 mxf
 * @创建日期 2021-03-05 14:02
 **/
@Service
public class PermissionServiceImpl implements PermissionService {
    private static final Logger logger = LoggerFactory.getLogger(PermissionServiceImpl.class);

    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private HostHolder hostHolder;

    @Override
    public PageDataResult getPermissionList(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<PermissionDTO> permissions = permissionMapper.getPermissionList();

        PageHelper.startPage(pageNum, pageSize);

        if(permissions.size() != 0){
            PageInfo<PermissionDTO> pageInfo = new PageInfo<>(permissions);
            pageDataResult.setList(permissions);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public List<PermissionDTO> parentPermissionList() {
        return permissionMapper.parentPermissionList();
    }

    @Override
    public Map<String, Object> addPermission(TSPermission permission) {
        Map<String,Object> data = new HashMap();
        try {
            //默认字段
            permission.setCreateDate(DateUtils.getCurrentDateToDate());
            permission.setCreateBy(hostHolder.getUser().getUserName());
            permission.setCreateName(hostHolder.getUser().getRealName());
            permission.setUpdateDate(DateUtils.getCurrentDateToDate());
            permission.setUpdateBy(hostHolder.getUser().getUserName());
            permission.setUpdateName(hostHolder.getUser().getRealName());
            permission.setDeleted(1);

            int result = permissionMapper.insertPermission(permission);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("权限[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            logger.info("权限[新增]，结果=新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("权限[新增]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> updatePermission(TSPermission permission) {
        Map<String,Object> data = new HashMap();
        try{
            permission.setUpdateName(hostHolder.getUser().getRealName());
            permission.setUpdateBy(hostHolder.getUser().getUserName());
            permission.setUpdateDate(DateUtils.getCurrentDateToDate());
            int result = permissionMapper.updatePermission(permission);
            if(result == 0){
                data.put("code",0);
                data.put("msg","更新失败！");
                logger.error("权限[更新]，结果=更新失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","更新成功！");
            logger.info("权限[更新]，结果=更新成功！");
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("权限[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> del(Integer id) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除权限菜单
            int result = permissionMapper.deleteByPrimaryKey(id);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除失败");
                logger.error("删除失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除成功");
            logger.info("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除权限菜单异常！", e);
        }
        return data;
    }
}
